<?php

class AjaxAction extends PublicAction
{

    public function area()
    {
        $module = M('area');
        $request = $_REQUEST;
        $id = intval($request['id']);
        $level = intval($request['level']);
        $provinceid = intval($request['provinceid']);
        $cityid = intval($request['cityid']);
        $areaid = intval($request['areaid']);

        $province_str = '<option value="0">请选择省份...</option>';
        $city_str = '<option value="0">请选择城市...</option>';
        $area_str = '<option value="0">请选择区域...</option>';
        $str = '';

        $r = $module->where("parentid='" . $id . "'")->select();

        foreach ($r as $key => $pro) {
            $selected = ($pro['id'] == $provinceid) ? ' selected="selected" ' : '';
            $str .= '<option value="' . $pro['id'] . '"' . $selected . '>' . $pro['name'] . '</option>';
        }
        if ($level == 0) {
            $province_str .= $str;
        } elseif ($level == 1) {
            $city_str .= $str;
        } elseif ($level == 2) {
            $area_str .= $str;
        }
        $str = '';
        if ($provinceid) {

            $rr = $module->where("parentid=" . $provinceid)->select();
            foreach ($rr as $key => $pro) {
                $selected = ($pro['id'] == $cityid) ? ' selected="selected" ' : '';
                $str .= '<option value="' . $pro['id'] . '"' . $selected . '>' . $pro['name'] . '</option>';
            }
            $city_str .= $str;
        }
        $str = '';
        if ($cityid) {
            $rrr = $module->where("parentid=" . $cityid)->select();
            foreach ($rrr as $key => $pro) {
                $selected = ($pro['id'] == $areaid) ? ' selected="selected" ' : '';
                $str .= '<option value="' . $pro['id'] . '"' . $selected . '>' . $pro['name'] . '</option>';
            }
            $area_str .= $str;
        }

        $res = array();
        $res['data'] = $rs ? 1 : 0;
        $res['province'] = $province_str;
        $res['city'] = $city_str;
        $res['area'] = $area_str;

        echo json_encode($res);
    }

    public function address()
    {
        $do = I('do','', 'trim');
        $model = M('UserAddress');
        $id = I('id', 0, 'intval');

        $provinceid = intval($_REQUEST['province']);
        $cityid = intval($_REQUEST['city']);
        $areaid = intval($_REQUEST['area']);

        $userid = $_POST['userid'] = session('user.id');
        if ($do == 'save') {
            $id = intval($_POST['id']);
            $_POST['isdefault'] = 1;
            if ($userid) {
                $model->where("user_id=" . $userid)->save(array('isdefault' => 0));
                if ($id) {
                    $r = $model->save($_POST);
                    if ($model->getDbError()) {
                        die(json_encode(array('id' => 0)));
                    }
                    $_POST['edit'] = 1;
                } else {
                    $where['province'] = array('eq', $provinceid);
                    $where['city'] = array('eq', $cityid);
                    $where['area'] = array('eq', $areaid);
                    $where['consignee'] = array('eq', $_POST['consignee']);
                    $where['address'] = array('eq', $_POST['address']);
                    $ir = $model->where($where)->find();
                    if ($ir) {
                        echo json_encode(array('error' => '收货信息已经存在！'));
                        exit;
                    }
                    $id = $model->add($_POST);
                }
            } else {
                $_POST['id'] = 1;
                $data = serialize($_POST);
                cookie('guest_address', $data, 315360000);
                $id = 1;
                $_POST['edit'] = 1;
            }
            if ($id) {
                $_POST['id'] = $id;
                die(json_encode($_POST));
            } else {
                die(json_encode(array('id' => 0)));
            }

        } elseif ($do == 'get') {
            if ($userid) {
                $data = $model->find($id);
            } else {
                $data = unserialize(cookie('guest_address'));
            }
            if ($data) {
                die(json_encode($data));
            } else {
                die(json_encode(array('id' => 0)));
            }
        }
    }

    public function shipping()
    {
        $do = $_REQUEST['do'];
        $model = M('Shipping');
        $id = intval($_REQUEST['id']);

        if ($do == 'get') {
            $data = $model->find($id);
            if ($data) {
                echo json_encode($data);
            } else {
                echo json_encode(array('id' => 0));
            }
            exit;
        }
    }
}
